const http = require('http')
const fs = require('fs')
const path = require('path')
const url = require('url')
const qs = require('querystring')

const server = http.createServer((req,res) => {
  // xxxx?a=1&b=2
  const [url, queryStr] = req.url.split('?')
  console.log(url, queryStr)
  console.log(qs.parse(queryStr))
  if(url === '/' || url === '/index.html') {
    const filePath = path.join(__dirname, 'index.html')
    console.log(filePath)
    const result = fs.readFileSync(filePath)
    res.setHeader('content-type', 'text/css;charset=utf8')
    res.end(result)
  } else if(url === '/jd') {
    res.statusCode = 301
    res.setHeader('location', 'http://www.jd.com')
    res.end()
  }
})

server.listen(8001)

